package org.jeecg.alibaba.api;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.jeecg.alibaba.utils.HttpUtils;
import org.jeecg.alibaba.vo.IdCardVo;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.BeanUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *  【身份证OCR文字识别】身份证OCR识别
 *  https://market.aliyun.com/products/57124001/cmapi00035059.html?spm=5176.2020520132.101.3.471c7218m7iNcf#sku=yuncode2905900001
 */
public class IdCardOCR {

    /**
     * 获取身份证正面照数据
     * @param id_card_z 身份证正面地址
     * @return
     */
    public static IdCardVo queryIdCardZ(String id_card_z){
        try {
            //身份证正面数据
            JSONObject jsonObject = getOCRData(id_card_z);
            if (oConvertUtils.isNotEmpty(jsonObject)) {
                IdCardVo idCardVo = new IdCardVo();
                idCardVo.setName(jsonObject.getString("name"));
                if ("男".equals(jsonObject.getString("sex"))) {
                    idCardVo.setSex(1);
                } else if ("女".equals(jsonObject.getString("sex"))) {
                    idCardVo.setSex(2);
                } else {
                    idCardVo.setSex(0);
                }
                idCardVo.setNation(jsonObject.getString("nation"));
                String year = jsonObject.getString("year");
                String month = jsonObject.getString("month");
                String day = jsonObject.getString("day");
                idCardVo.setBirthday(DateUtils.sdf4.parse(year + "-" + month + "-" + day));
                idCardVo.setNumber(jsonObject.getString("number"));
                idCardVo.setAddress(jsonObject.getString("address"));
                return idCardVo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取身份证反面照数据
     * @param id_card_f 身份证反面地址
     * @return
     */
    public static IdCardVo queryIdCardF(String id_card_f){
        try {
            //身份证反面数据
            JSONObject jsonObject = getOCRData(id_card_f);
            if (oConvertUtils.isNotEmpty(jsonObject)) {
                IdCardVo idCardVo = new IdCardVo();
                idCardVo.setAuthority(jsonObject.getString("authority"));
                idCardVo.setTimelimit(jsonObject.getString("timelimit"));
                return idCardVo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 通过网络图片地址获取身份证上面的数据
     * @param image_url 图片网络地址，采用抓取网络图片方式时需选取此参数
     * @return
     */
    private static JSONObject getOCRData(String image_url){
        String host = "https://dfidcard.market.alicloudapi.com";
        String path = "/ocr/idcard";
        String method = "POST";
        String appCode = "4cd2142e6c4a49f18ea423cf7c294bd6";//阿里云账号：178900978@qq.com【程雷】
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("image_url", image_url);

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
            String entity = EntityUtils.toString(response.getEntity());
            //String转JSONObject
            JSONObject jsonObject = JSONObject.parseObject(entity);
            //返回结果
            if("OK".equals(jsonObject.getString("status"))){
                String result = jsonObject.getString("result");
                JSONObject json_result = JSONObject.parseObject(result);
                return json_result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println("==身份证正面==>>>>>：" + queryIdCardZ("https://img.51xinlong.com/xlminiapp/customer/1670831468438.7463.jpg"));
        System.out.println("==身份证反面==>>>>>：" + queryIdCardF("https://img.51xinlong.com/xlminiapp/customer/1670831478445.1243.jpg"));
    }

}
